'use client';

// RHF
import { FormProvider, useForm } from 'react-hook-form';

// Zod
import { zodResolver } from '@hookform/resolvers/zod';

// Types
import { InvoiceType } from '@/types';
import { InvoiceSchema } from '@/lib/schemas';
import { FORM_DEFAULT_VALUES } from '@/lib/variables';

import { ThemeProvider } from './ThemeProvider';
import { TranslationProvider } from './TranslationContext';
import { InvoiceContextProvider } from './InvoiceContext';
import { ChargesContextProvider } from './ChargesContext';

type ProvidersProps = {
    children: React.ReactNode;
};

export const Providers = ({ children }: ProvidersProps) => {
    const form = useForm<InvoiceType>({
        resolver: zodResolver(InvoiceSchema),
        defaultValues: FORM_DEFAULT_VALUES,
    });

    return (
        <ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
            <TranslationProvider>
                <FormProvider {...form}>
                    <InvoiceContextProvider>
                        <ChargesContextProvider>
                            <>{children}</>
                        </ChargesContextProvider>
                    </InvoiceContextProvider>
                </FormProvider>
            </TranslationProvider>
        </ThemeProvider>
    );
};
